home *** CD-ROM | disk | FTP | other *** search
/ Champak 141 / (Vol 141) Oct 17 2011.iso / Games / Clueless.swf / scripts / Common / ParticleSystem / Emitter.as < prev    next >
Encoding:
Text File  |  2011-10-17  |  16.4 KB  |  548 lines

  1. package Common.ParticleSystem
  2. {
  3.    import Common.FastMath;
  4.    import flash.display.Bitmap;
  5.    import flash.display.Loader;
  6.    import flash.display.MovieClip;
  7.    import flash.display.Sprite;
  8.    import flash.events.Event;
  9.    import flash.events.IOErrorEvent;
  10.    import flash.geom.Point;
  11.    import flash.net.URLRequest;
  12.    
  13.    public class Emitter extends Sprite
  14.    {
  15.       
  16.       public static const C_PARTICLE_ELLIPSE:* = 2;
  17.       
  18.       public static const C_PARTICLE_POINT:* = 0;
  19.       
  20.       public static const C_PARTICLE_LINE:* = 3;
  21.       
  22.       public static const C_PARTICLE_AREA:* = 1;
  23.        
  24.       
  25.       private var tintBVar:Number;
  26.       
  27.       private var active:Boolean;
  28.       
  29.       private var wind:Point;
  30.       
  31.       private var tintRVar:Number;
  32.       
  33.       private var velocityVar:Number;
  34.       
  35.       private var life:Number;
  36.       
  37.       private var durationStart:uint;
  38.       
  39.       private var fadeOut:Boolean;
  40.       
  41.       private var spriteLoader:Loader;
  42.       
  43.       private var gravity:Point;
  44.       
  45.       private var rateStart:int;
  46.       
  47.       private var spawnType:uint;
  48.       
  49.       private var weight:Number;
  50.       
  51.       private var spread:int;
  52.       
  53.       private var scale:Number;
  54.       
  55.       private var scaleVar:Number;
  56.       
  57.       private var scaleDeath:Number;
  58.       
  59.       private var weightVar:Number;
  60.       
  61.       private var direction:int;
  62.       
  63.       private var bitmapCache:Boolean;
  64.       
  65.       private var tintB:Number;
  66.       
  67.       private var maxParticles:uint;
  68.       
  69.       private var duration:uint;
  70.       
  71.       private var particles:Array;
  72.       
  73.       private var simSpeed:Number;
  74.       
  75.       private var tintG:Number;
  76.       
  77.       private var rotationRate:Number;
  78.       
  79.       private var pointB:Point;
  80.       
  81.       private var affectors:Array;
  82.       
  83.       private var fadeIn:Boolean;
  84.       
  85.       private var rotateAuto:Boolean;
  86.       
  87.       private var pointA:Point;
  88.       
  89.       private var tintR:Number;
  90.       
  91.       private var rotationRateVar:Number;
  92.       
  93.       private var tintGVar:Number;
  94.       
  95.       private var velocity:Number;
  96.       
  97.       private var rate:int;
  98.       
  99.       private var lifeVar:Number;
  100.       
  101.       private var friction:Number;
  102.       
  103.       public function Emitter()
  104.       {
  105.          super();
  106.          particles = new Array();
  107.          affectors = new Array();
  108.          spawnType = 0;
  109.          mouseEnabled = false;
  110.          spriteLoader = new Loader();
  111.          pointA = new Point(0,0);
  112.          pointB = new Point(10,10);
  113.          life = 30;
  114.          lifeVar = 20;
  115.          rate = 30;
  116.          simSpeed = 1;
  117.          duration = 1;
  118.          bitmapCache = false;
  119.          maxParticles = 200;
  120.          weight = 1;
  121.          weightVar = 0;
  122.          velocity = 30;
  123.          velocityVar = 10;
  124.          rotationRate = 5;
  125.          rotationRateVar = 10;
  126.          rotateAuto = false;
  127.          friction = 0.1;
  128.          direction = 180;
  129.          spread = 360;
  130.          gravity = new Point(0,0);
  131.          wind = new Point(0,0);
  132.          fadeIn = false;
  133.          fadeOut = false;
  134.          scale = 1;
  135.          scaleVar = 0;
  136.          scaleDeath = 1;
  137.          tintR = 0;
  138.          tintRVar = 0;
  139.          tintG = 0;
  140.          tintGVar = 0;
  141.          tintB = 0;
  142.          tintBVar = 0;
  143.       }
  144.       
  145.       public static function fromXML(param1:XML) : Emitter
  146.       {
  147.          var _loc2_:Emitter = null;
  148.          var _loc3_:XML = null;
  149.          var _loc4_:String = null;
  150.          var _loc5_:String = null;
  151.          _loc2_ = new Emitter();
  152.          for each(_loc3_ in param1.attributes())
  153.          {
  154.             _loc4_ = _loc3_;
  155.             _loc5_ = String(_loc3_.name());
  156.             switch(_loc5_)
  157.             {
  158.                case "maxParticles":
  159.                   _loc2_.maxParticles = uint(_loc4_);
  160.                   break;
  161.                case "sprite":
  162.                   _loc2_.loadSprite(_loc4_);
  163.                   break;
  164.                case "blendMode":
  165.                   _loc2_.blendMode = _loc4_;
  166.                   break;
  167.                case "spawnType":
  168.                   _loc2_.spawnType = Emitter.getParticleType(_loc4_);
  169.                   break;
  170.                case "duration":
  171.                   _loc2_.durationStart = uint(_loc4_);
  172.                   break;
  173.                case "weight":
  174.                   _loc2_.weight = Number(_loc4_);
  175.                   break;
  176.                case "weightVar":
  177.                   _loc2_.weightVar = Number(_loc4_);
  178.                   break;
  179.                case "velocity":
  180.                   _loc2_.velocity = Number(_loc4_);
  181.                   break;
  182.                case "velocityVar":
  183.                   _loc2_.velocityVar = Number(_loc4_);
  184.                   break;
  185.                case "rotationRate":
  186.                   _loc2_.rotationRate = Number(_loc4_);
  187.                   break;
  188.                case "rotationRateVar":
  189.                   _loc2_.rotationRateVar = Number(_loc4_);
  190.                   break;
  191.                case "rotateAuto":
  192.                   _loc2_.rotateAuto = _loc4_ == "true";
  193.                   break;
  194.                case "friction":
  195.                   _loc2_.friction = Number(_loc4_);
  196.                   break;
  197.                case "direction":
  198.                   _loc2_.direction = Number(_loc4_);
  199.                   break;
  200.                case "spread":
  201.                   _loc2_.spread = Number(_loc4_);
  202.                   break;
  203.                case "life":
  204.                   _loc2_.life = Number(_loc4_);
  205.                   break;
  206.                case "lifeVar":
  207.                   _loc2_.lifeVar = Number(_loc4_);
  208.                   break;
  209.                case "rate":
  210.                   _loc2_.rateStart = int(_loc4_);
  211.                   break;
  212.                case "fadeIn":
  213.                   _loc2_.fadeIn = _loc4_ == "true";
  214.                   break;
  215.                case "fadeOut":
  216.                   _loc2_.fadeOut = _loc4_ == "true";
  217.                   break;
  218.                case "scale":
  219.                   _loc2_.scale = Number(_loc4_);
  220.                   break;
  221.                case "scaleVar":
  222.                   _loc2_.scaleVar = Number(_loc4_);
  223.                   break;
  224.                case "scaleDeath":
  225.                   _loc2_.scaleDeath = Number(_loc4_);
  226.                   break;
  227.                case "tintR":
  228.                   _loc2_.tintR = Number(_loc4_);
  229.                   break;
  230.                case "tintG":
  231.                   _loc2_.tintG = Number(_loc4_);
  232.                   break;
  233.                case "tintB":
  234.                   _loc2_.tintB = Number(_loc4_);
  235.                   break;
  236.                case "tintRVar":
  237.                   _loc2_.tintRVar = Number(_loc4_);
  238.                   break;
  239.                case "tintGVar":
  240.                   _loc2_.tintGVar = Number(_loc4_);
  241.                   break;
  242.                case "tintBVar":
  243.                   _loc2_.tintBVar = Number(_loc4_);
  244.                   break;
  245.                case "gravityX":
  246.                   _loc2_.gravity.x = Number(_loc4_);
  247.                   break;
  248.                case "gravityY":
  249.                   _loc2_.gravity.y = Number(_loc4_);
  250.                   break;
  251.                case "windX":
  252.                   _loc2_.wind.x = Number(_loc4_);
  253.                   break;
  254.                case "windY":
  255.                   _loc2_.wind.y = Number(_loc4_);
  256.                   break;
  257.             }
  258.          }
  259.          return _loc2_;
  260.       }
  261.       
  262.       public static function clone(param1:Emitter) : Emitter
  263.       {
  264.          var _loc2_:Emitter = null;
  265.          var _loc3_:* = undefined;
  266.          _loc2_ = new Emitter();
  267.          _loc2_.spawnType = param1.spawnType;
  268.          _loc2_.PointA.x = param1.PointA.x;
  269.          _loc2_.PointA.y = param1.PointA.y;
  270.          _loc2_.PointB.x = param1.PointB.x;
  271.          _loc2_.pointB.y = param1.PointB.y;
  272.          _loc2_.life = param1.life;
  273.          _loc2_.lifeVar = param1.lifeVar;
  274.          _loc2_.rate = param1.rate;
  275.          _loc2_.rateStart = param1.rateStart;
  276.          _loc2_.simSpeed = param1.simSpeed;
  277.          _loc2_.duration = param1.duration;
  278.          _loc2_.durationStart = param1.durationStart;
  279.          _loc2_.maxParticles = param1.maxParticles;
  280.          _loc2_.weight = param1.weight;
  281.          _loc2_.weightVar = param1.weightVar;
  282.          _loc2_.velocity = param1.velocity;
  283.          _loc2_.velocityVar = param1.velocityVar;
  284.          _loc2_.rotationRate = param1.rotationRate;
  285.          _loc2_.rotationRateVar = param1.rotationRateVar;
  286.          _loc2_.rotateAuto = param1.rotateAuto;
  287.          _loc2_.friction = param1.friction;
  288.          _loc2_.direction = param1.direction;
  289.          _loc2_.spread = param1.spread;
  290.          _loc2_.gravity.x = param1.gravity.x;
  291.          _loc2_.gravity.y = param1.gravity.y;
  292.          _loc2_.wind.x = param1.wind.x;
  293.          _loc2_.wind.y = param1.wind.y;
  294.          _loc2_.fadeIn = param1.fadeIn;
  295.          _loc2_.fadeOut = param1.fadeOut;
  296.          _loc2_.scale = param1.scale;
  297.          _loc2_.scaleVar = param1.scaleVar;
  298.          _loc2_.scaleDeath = param1.scaleDeath;
  299.          _loc2_.tintR = param1.tintR;
  300.          _loc2_.tintRVar = param1.tintRVar;
  301.          _loc2_.tintG = param1.tintG;
  302.          _loc2_.tintGVar = param1.tintGVar;
  303.          _loc2_.tintB = param1.tintB;
  304.          _loc2_.tintBVar = param1.tintBVar;
  305.          _loc3_ = Particle(param1.getChildAt(0)).getChildAt(0);
  306.          if(!(_loc3_ is Bitmap))
  307.          {
  308.             _loc3_ = Object(_loc3_).constructor;
  309.          }
  310.          _loc2_.initParticles(_loc3_);
  311.          return _loc2_;
  312.       }
  313.       
  314.       public static function getParticleType(param1:String) : uint
  315.       {
  316.          return uint(Emitter[param1]);
  317.       }
  318.       
  319.       public function startEmission() : void
  320.       {
  321.          Active = visible = true;
  322.          rate = rateStart;
  323.          duration = durationStart;
  324.       }
  325.       
  326.       public function get Active() : Boolean
  327.       {
  328.          return active;
  329.       }
  330.       
  331.       public function set Active(param1:Boolean) : *
  332.       {
  333.          this.active = param1;
  334.       }
  335.       
  336.       private function spawn(param1:Particle) : void
  337.       {
  338.          var _loc2_:Point = null;
  339.          param1.reset();
  340.          param1.Active = true;
  341.          param1.Weight = FastMath.variance(weight,weightVar);
  342.          param1.setVelocityDir(FastMath.variance(velocity,velocityVar),int(Math.floor(FastMath.variance(direction,spread,0.5))));
  343.          param1.revive(life,lifeVar);
  344.          _loc2_ = new Point(0,0);
  345.          switch(spawnType)
  346.          {
  347.             case C_PARTICLE_AREA:
  348.                _loc2_ = FastMath.magDir(Math.random(),Math.floor(Math.random() * 360));
  349.                _loc2_.x *= pointB.x;
  350.                _loc2_.y *= pointB.y;
  351.                _loc2_.offset(pointA.x,pointA.y);
  352.                break;
  353.             case C_PARTICLE_ELLIPSE:
  354.                _loc2_.x = FastMath.variance(0,1,0.5);
  355.                _loc2_.y = FastMath.variance(0,1,0.5);
  356.                _loc2_.normalize(1);
  357.                _loc2_.x *= pointB.x;
  358.                _loc2_.y *= pointB.y;
  359.                _loc2_.offset(pointA.x,pointA.y);
  360.                break;
  361.             case C_PARTICLE_LINE:
  362.                _loc2_ = Point.interpolate(pointA,pointB,Math.random());
  363.                break;
  364.             default:
  365.                _loc2_ = pointA;
  366.          }
  367.          param1.x = _loc2_.x;
  368.          param1.y = _loc2_.y;
  369.          param1.RotationRate = FastMath.variance(rotationRate,rotationRateVar);
  370.          param1.RotateAuto = rotateAuto;
  371.          param1.ScaleBirth = FastMath.variance(scale,scaleVar);
  372.          param1.ScaleDeath = scaleDeath;
  373.          param1.blendMode = blendMode;
  374.       }
  375.       
  376.       public function handleIOError(param1:IOErrorEvent) : void
  377.       {
  378.          trace("Load failed: IO error: " + param1.text);
  379.       }
  380.       
  381.       public function initContainer(param1:Event) : void
  382.       {
  383.          var _loc2_:Class = null;
  384.          var _loc3_:Bitmap = null;
  385.          bitmapCache = spriteLoader.content is Bitmap;
  386.          if(bitmapCache)
  387.          {
  388.             initParticles(Bitmap(param1.target.loader.content));
  389.          }
  390.          else
  391.          {
  392.             initParticles(Object(MovieClip(spriteLoader.content).getChildAt(0)).constructor);
  393.          }
  394.       }
  395.       
  396.       public function initParticles(param1:*) : *
  397.       {
  398.          var _loc2_:* = undefined;
  399.          var _loc3_:Particle = null;
  400.          var _loc4_:Bitmap = null;
  401.          _loc2_ = 0;
  402.          while(_loc2_ < maxParticles)
  403.          {
  404.             _loc3_ = new Particle();
  405.             if(param1 is Bitmap)
  406.             {
  407.                (_loc4_ = new Bitmap(param1.bitmapData)).x = param1.width * -0.5;
  408.                _loc4_.y = param1.height * -0.5;
  409.                _loc3_.addChild(_loc4_);
  410.                _loc3_.cacheAsBitmap = true;
  411.             }
  412.             else
  413.             {
  414.                _loc3_.addChild(new param1());
  415.             }
  416.             addChild(_loc3_);
  417.             _loc3_.Index = _loc2_;
  418.             _loc3_.Active = false;
  419.             _loc3_.Life = 0;
  420.             particles[_loc2_] = _loc3_;
  421.             _loc2_++;
  422.          }
  423.       }
  424.       
  425.       public function loadSprite(param1:String) : void
  426.       {
  427.          var _loc2_:String = null;
  428.          _loc2_ = "Assets/Particles/Sprites/" + param1;
  429.          spriteLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,initContainer);
  430.          spriteLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,handleIOError);
  431.          spriteLoader.load(new URLRequest(_loc2_));
  432.       }
  433.       
  434.       public function get PointA() : Point
  435.       {
  436.          return pointA;
  437.       }
  438.       
  439.       public function get PointB() : Point
  440.       {
  441.          return pointB;
  442.       }
  443.       
  444.       public function stopEmission() : void
  445.       {
  446.          var _loc1_:int = 0;
  447.          var _loc2_:* = undefined;
  448.          Active = visible = false;
  449.          rate = 0;
  450.          duration = 1;
  451.          _loc1_ = this.numChildren;
  452.          while(--_loc1_ != -1)
  453.          {
  454.             _loc2_ = getChildAt(_loc1_);
  455.             _loc2_.visible = false;
  456.             removeChildAt(_loc1_);
  457.          }
  458.       }
  459.       
  460.       public function update() : void
  461.       {
  462.          var _loc1_:uint = 0;
  463.          var _loc2_:Boolean = false;
  464.          var _loc3_:Particle = null;
  465.          if(duration > 1)
  466.          {
  467.             if(--duration == 1)
  468.             {
  469.                Active = false;
  470.             }
  471.          }
  472.          _loc1_ = uint(rate);
  473.          _loc2_ = true;
  474.          for each(_loc3_ in particles)
  475.          {
  476.             if(_loc3_.Active)
  477.             {
  478.                applyForces(_loc3_);
  479.                _loc2_ = false;
  480.             }
  481.             else if(Active && _loc1_ > 0 && (duration > 1 || duration == 0))
  482.             {
  483.                _loc1_--;
  484.                spawn(_loc3_);
  485.                applyForces(_loc3_);
  486.                _loc2_ = false;
  487.             }
  488.          }
  489.          if(_loc2_)
  490.          {
  491.             stopEmission();
  492.          }
  493.       }
  494.       
  495.       private function applyForces(param1:Particle) : void
  496.       {
  497.          var _loc2_:Number = NaN;
  498.          var _loc3_:Number = NaN;
  499.          var _loc4_:Number = NaN;
  500.          var _loc5_:Number = NaN;
  501.          var _loc6_:Number = NaN;
  502.          var _loc7_:Number = NaN;
  503.          var _loc8_:Number = NaN;
  504.          _loc2_ = gravity.x + wind.x;
  505.          _loc3_ = gravity.y + wind.y;
  506.          _loc4_ = _loc2_ / param1.Weight;
  507.          _loc5_ = _loc3_ / param1.Weight;
  508.          _loc6_ = 1 - friction;
  509.          param1.Velocity.x = (param1.Velocity.x + _loc4_ * simSpeed) * _loc6_;
  510.          param1.Velocity.y = (param1.Velocity.y + _loc5_ * simSpeed) * _loc6_;
  511.          param1.x += param1.Velocity.x;
  512.          param1.y += param1.Velocity.y;
  513.          param1.rotate();
  514.          _loc7_ = param1.Age / param1.Life;
  515.          _loc8_ = 1;
  516.          if(fadeIn && fadeOut)
  517.          {
  518.             if(_loc7_ >= 0.5)
  519.             {
  520.                _loc8_ = (1 - _loc7_) * 2;
  521.             }
  522.             else
  523.             {
  524.                _loc8_ = _loc7_ * 2;
  525.             }
  526.          }
  527.          else if(fadeIn && !fadeOut)
  528.          {
  529.             _loc8_ = 1 - _loc7_;
  530.          }
  531.          else if(!fadeIn && fadeOut)
  532.          {
  533.             _loc8_ = _loc7_;
  534.          }
  535.          else
  536.          {
  537.             _loc8_ = 1;
  538.          }
  539.          param1.alpha = _loc8_;
  540.          param1.grow(1 - _loc7_);
  541.          if(--param1.Age <= 0)
  542.          {
  543.             param1.Active = false;
  544.          }
  545.       }
  546.    }
  547. }
  548.